package xyz.riceball.springcloudstreambinderfile.file;

import org.springframework.cloud.stream.binder.ExtendedConsumerProperties;
import org.springframework.cloud.stream.binder.ExtendedProducerProperties;
import org.springframework.cloud.stream.provisioning.ConsumerDestination;
import org.springframework.cloud.stream.provisioning.ProducerDestination;
import org.springframework.cloud.stream.provisioning.ProvisioningException;
import org.springframework.cloud.stream.provisioning.ProvisioningProvider;
import xyz.riceball.springcloudstreambinderfile.property.FileConsumerProperties;
import xyz.riceball.springcloudstreambinderfile.property.FileProducerProperties;

// @checkstyle:off
/**
 * <p>
 * FileProvisioningProvider
 * </p>
 *
 * @author xiaovcloud
 * @since 2024/6/13 13:57
 */
public class FileProvisioningProvider implements ProvisioningProvider<
	ExtendedConsumerProperties<FileConsumerProperties>, ExtendedProducerProperties<FileProducerProperties>> {

	public FileProvisioningProvider() {
		super();
	}

	@Override
	public ProducerDestination provisionProducerDestination(String name, ExtendedProducerProperties<FileProducerProperties> properties) throws ProvisioningException {
		return new FileMessageDestination(name);
	}


	@Override
	public ConsumerDestination provisionConsumerDestination(String name, String group, ExtendedConsumerProperties<FileConsumerProperties> properties) throws ProvisioningException {
		return new FileMessageDestination(name);
	}


	private static class FileMessageDestination implements ProducerDestination, ConsumerDestination {

		private final String destination;

		private FileMessageDestination(final String destination) {
			this.destination = destination;
		}

		@Override
		public String getName() {
			return destination.trim();
		}

		@Override
		public String getNameForPartition(int partition) {
			throw new UnsupportedOperationException("Partitioning is not implemented for file messaging.");
		}

	}

}
// @checkstyle:on
